home *** CD-ROM | disk | FTP | other *** search
/ Aminet 31 / Aminet 31 (1999)(Schatztruhe)[!][Jun 1999].iso / Aminet / dev / c / vbccwossrc.lha / vbcc / pasm / ppcasm.h < prev    next >
C/C++ Source or Header  |  1999-03-07  |  24KB  |  658 lines

  1. /* $VER: pasm ppcasm.h V1.2a (16.01.99)
  2.  *
  3.  * This file is part of pasm, a portable PowerPC assembler.
  4.  * Copyright (c) 1997-98  Frank Wille
  5.  *
  6.  * pasm is freeware and part of the portable and retargetable ANSI C
  7.  * compiler vbcc, copyright (c) 1995-98 by Volker Barthelmann.
  8.  * pasm may be freely redistributed as long as no modifications are
  9.  * made and nothing is charged for it. Non-commercial usage is allowed
  10.  * without any restrictions.
  11.  * EVERY PRODUCT OR PROGRAM DERIVED DIRECTLY FROM MY SOURCE MAY NOT BE
  12.  * SOLD COMMERCIALLY WITHOUT PERMISSION FROM THE AUTHOR.
  13.  *
  14.  *
  15.  * v1.2a  (16.01.99) phx
  16.  *        Compiles on SGI Irix 5.3.
  17.  * v1.2   (24.10.98) phx
  18.  *        @sdarx small data support.
  19.  *        An empty section with reloc symbols will no longer be deleted.
  20.  * v1.1d  (27.09.98) phx
  21.  *        Rudimentary PPC4xx support (contributed by Jeff Bevis - jab).
  22.  *        Should compile on a (gasp!) Wintel-box.
  23.  * v1.1c  (22.09.98) phx
  24.  *        Include file name was trashed.
  25.  * v1.1b  (05.07.98) phx
  26.  *        Introduced section flags SF_CHIP and SF_FAST for EHF/ADos. They
  27.  *        are set by the new section attributes "C" and "F".
  28.  * v1.1a  (20.06.98) phx
  29.  *        F_UPDATE. Check rA = rD for update instructions.
  30.  * v1.1   (19.06.98) phx
  31.  *        Multiple small data sections are supported. The new directive
  32.  *        .sdreg should be used to set the base register.
  33.  *        Set R_PPC_TOC16 to R_PPC_SDAREL16.
  34.  * v1.0   (24.05.98) phx
  35.  *        Hopefully all known bugs are fixed now and pasm is usable
  36.  *        on any platform, big endian, little endian and 64-bit.
  37.  *        Fixed some more bugs in the ELF output file generator and
  38.  *        some wrong instructions.
  39.  * v0.9   (03.04.98) phx
  40.  *        ELF bug fix: pasm generated WEAK symbols, instead of GLOBAL ones!
  41.  * v0.8   (14.02.98) phx
  42.  *        Alignment list for each section. This fixes the problems
  43.  *        with optimizations.
  44.  * v0.7   (02.01.98) phx
  45.  *        Define "NetBSDAmiga68k" changed to "NetBSD68k".
  46.  *        Changed ParsedLine (next) and GlobalVars (anotherpass) to
  47.  *        allow more than two assembler passes - as required for
  48.  *        optimizations.
  49.  *        search_instr() is global.
  50.  *        Output format 3 is ADOS (like EHF, but doesn't use HUNK_PPC_CODE).
  51.  * v0.6   (30.10.97) phx
  52.  *        More options. GlobalVars: optinstrmode and supermode.
  53.  * v0.5   (12.10.97) phx
  54.  *        Add userdeflist and usrdefs to GlobalVars for symbol definitions
  55.  *        via the command line.
  56.  *        .set allows symbols to be reused.
  57.  *        Last line of a source text was ignored, if newline is missing.
  58.  * v0.4   (05.07.97) phx
  59.  *        Program returns EXIT_FAILURE if an error occurs.
  60.  *        Base address for absolute code may be set with -B option.
  61.  *        EHF support.
  62.  *        Added R_PPC_TOC16 relocation type.
  63.  *        Option -x automatically declares unknown symbols as
  64.  *        externally defined. New GlobalVars entry: autoextern.
  65.  *        Runs on Linux/DEC-Alpha with 64-bit integers.
  66.  *        Changed program name from "PPCasm" to "pasm". Reason: There
  67.  *        is already a PPCasm for Apple Macintosh.
  68.  * v0.3   (20.04.97) phx
  69.  *        Using correct names for PowerPC relocations.
  70.  *        Added little-endian conversion macros.
  71.  * v0.2   (25.03.97) phx
  72.  *        Writes ELF object for 32-bit PowerPC big-endian. Either absolute
  73.  *        or ELF output format may be selected. ELF is default for all
  74.  *        currently supported platforms. PPCasm supports nine different
  75.  *        relocation types (there are much more...).
  76.  *        Compiles and works also under NetBSD/amiga (68k).
  77.  *        Changed function declaration to 'new style' in all sources
  78.  *        (to avoid problems with '...' for example).
  79.  *        Included NetBSD/amiga as supported architecture.
  80.  * v0.1   (11.03.97) phx
  81.  *        First test version with all PowerPC instructions and most
  82.  *        important directives. Only raw, absolute output.
  83.  * v0.0   (14.02.97) phx
  84.  *        File created. Project started.
  85.  */
  86.  
  87. #include <stdlib.h>
  88. #include <stdio.h>
  89. #include <string.h>
  90. #include <stdarg.h>
  91. #include <ctype.h>
  92.  
  93. /* program's name */
  94. #define PNAME "pasm"
  95.  
  96. /* version/revision */
  97. #define VERSION 1
  98. #define REVISION 2
  99. #define PLEVEL 2
  100.  
  101. /* architecture specific defines */
  102. #if defined (AmigaOS68k)
  103. #define MACHINE "Amiga OS/M68k"
  104. #define BIGENDIAN
  105. #define STDTYPES
  106. #elif defined (AmigaOSPPC)
  107. #define MACHINE "Amiga OS/PowerPC"
  108. #define BIGENDIAN
  109. #define STDTYPES
  110. #elif defined (NetBSD68k)
  111. #define MACHINE "NetBSD/M68k"
  112. #define BIGENDIAN
  113. #define STDTYPES
  114. #elif defined (SolarisSparc)
  115. #define MACHINE "Solaris/Sparc"
  116. #define BIGENDIAN
  117. #define STDTYPES
  118. #elif defined (SunOSSparc)
  119. #define MACHINE "SunOS/Sparc"
  120. #define BIGENDIAN
  121. #define STDTYPES
  122. #elif defined (SCOi386)
  123. #define MACHINE "SCO/i386"
  124. #define LITTLEENDIAN
  125. #define STDTYPES
  126. #elif defined (Linuxi386)
  127. #define MACHINE "Linux/i386"
  128. #define LITTLEENDIAN
  129. #define STDTYPES
  130. #elif defined (LinuxAlpha)
  131. #define MACHINE "Linux/Alpha"
  132. #define LITTLEENDIAN
  133. #define TYPES64BIT
  134. #elif defined (Wintel)  /* jab */
  135. #define MACHINE "Wintel"
  136. #define LITTLEENDIAN
  137. #define STDTYPES
  138. #elif defined (Irix53)
  139. #define MACHINE "Irix5.3/mips"
  140. #define BIGENDIAN
  141. #define STDTYPES
  142. #else
  143. #error Unsupported architecture! Please adapt the source text.
  144. #endif
  145.  
  146. #ifdef STDTYPES
  147. typedef signed char int8;
  148. typedef unsigned char uint8;
  149. typedef signed short int int16;
  150. typedef unsigned short int uint16;
  151. typedef signed long int int32;
  152. typedef unsigned long int uint32;
  153. typedef signed char bool;
  154. #elif defined (TYPES64BIT)
  155. typedef signed char int8;
  156. typedef unsigned char uint8;
  157. typedef signed short int16;
  158. typedef unsigned short uint16;
  159. typedef signed int int32;
  160. typedef unsigned int uint32;
  161. typedef int bool;
  162. #else
  163. #error Unsupported architecture! Please adapt the source text.
  164. #endif
  165.  
  166. /* endian conversion */
  167. #if defined (BIGENDIAN)
  168. #define ECH(x) x
  169. #define ECW(x) x
  170. #define ECVH(x) x
  171. #define ECVW(x) x
  172. #elif defined (LITTLEENDIAN)
  173. #define ECH(x) (((x)&0xff)<<8|((x)&0xff00)>>8)
  174. #define ECW(x) (((x)&0xff)<<24|((x)&0xff00)<<8|((x)&0xff0000)>>8|((x)&0xff000000)>>24)
  175. #define ECVH(x) l2bh(x)
  176. #define ECVW(x) l2bw(x)
  177. #else
  178. #error You have to define either BIGENDIAN or LITTLEENDIAN.
  179. #endif
  180.  
  181.  
  182. /* program constants */
  183.  
  184. #ifndef TRUE
  185. #define TRUE 1
  186. #endif
  187. #ifndef FALSE
  188. #define FALSE 0
  189. #endif
  190. #ifndef NULL
  191. #define NULL 0
  192. #endif
  193.  
  194. #define FNAMEBUFSIZE 1024       /* buffer size for file names */
  195. #define EXPSTACKSIZE 32         /* maximum arguments in an expression */
  196.  
  197.  
  198. /* structures */
  199.  
  200. struct node {
  201.   struct node *next;
  202.   struct node *pred;
  203. };
  204.  
  205. struct list {
  206.   struct node *first;
  207.   struct node *dummy;
  208.   struct node *last;
  209. };
  210.  
  211. struct CPUInstr {
  212.   struct CPUInstr *hash_chain;  /* next instruction in hash chain */
  213.   char *name;                   /* instruction's name */
  214.   uint16 flags;                 /* format flags */
  215.   uint8 type;                   /* instruction format, see defines */
  216.   uint8 opcd;                   /* opcode (bit 0-5) */
  217.   uint8 fieldD;                 /* preset D field (bit 6-10) */
  218.   uint8 fieldA;                 /* preset D field (bit 11-15) */
  219.   uint16 xo;                    /* extended opcode (bit 21-31) */
  220. };
  221. /* CPU instruction formats */
  222. #define T_I 0                   /* Bx */
  223. #define T_B 1                   /* BCx */
  224. #define T_DD 2                  /* LWZ */
  225. #define T_DI 3                  /* ADDI */
  226. #define T_DS 4                  /* LD */
  227. #define T_X 5                   /* AND */
  228. #define T_IMM 6                 /* MTFSFI */
  229. #define T_XLB 7                 /* BCLRx */
  230. #define T_XSPR 8                /* MFSPR */
  231. #define T_XCRM 9                /* MTCRF */
  232. #define T_XFL 10                /* MTFSF */
  233. #define T_XS 11                 /* SRADIx */
  234. #define T_A 12                  /* FMADDx */
  235. #define T_M 13                  /* RLWIMIx */
  236. #define T_MD 14                 /* RLDICx */
  237. #define T_CMP 15                /* CMP */
  238. /* CPU instruction flags */
  239. #define F_SUPP_D 0x01           /* D = 0 */
  240. #define F_SUPP_A 0x02           /* A = 0 */
  241. #define F_SUPP_B 0x04           /* B = 0 */
  242. #define F_SUPP_C 0x08           /* C = 0 */
  243. #define F_CRF_D 0x10            /* D = CR field */
  244. #define F_CRF_S 0x20            /* S = CR field */